Page({
  /**
   * 页面的初始数据
   *  */
  data:{
    region:['安徽省','芜湖市','镜湖区'],
    now:{
      
      tmp:0,
      cond_text:'未知',
      cond_code:'999',
      hum:0,
      pres:0,
      vis:0,
      wind_dir:0,
      wind_spd:0,
      wind_sc:0
    }

  },
  /**
   * 更新省市区信息
   */
  regionChange:function(e){
    this.setData({region:e.detail.value});
    this.getWeather();//更新天气
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options){
    this.getWeather();//更新天气
  },
  /**
   * 获取实况天气数据
   */
  getWeather:function()
  {
    var that = this;
    wx.request({
      url: 'https://geoapi.qweather.com/v2/city/lookup',
      data: {
        location:that.data.region[1],
        key:'1c8587b396ff4fd481bd25e7b479b0f4'
      },
      success:function(res){
        console.log(res.data);
        wx.request({
          url: 'https://devapi.qweather.com/v7/weather/now',
          data: {
            location:res.data.location[0].id,
            key: '1c8587b396ff4fd481bd25e7b479b0f4'
          },
          success:function(res){
          console.log(res.data); 
          that.setData({
            tmp: res.data.now.temp,
            cond_text: res.data.now.text,
            cond_code: res.data.now.icon,
            hum: res.data.now.humidity,
            pres: res.data.now.pressure,
            vis: res.data.now.vis,
            wind_spd: res.data.now.windSpeed,
            wind_dir: res.data.now.windDir,
            wind_sc: res.data.now.windScale
          });
          }
        })
      },
      fail:(res)=>{
        console.log(res);
      }
    })
  },
})
